home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch1 / BitEdit.frm < prev    next >
Text File  |  1999-04-06  |  30KB  |  1,011 lines

  1. VERSION 5.00
  2. Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#2.0#0"; "MSCOMCTL.OCX"
  3. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  4. Begin VB.Form Form1 
  5.    Caption         =   "Form1"
  6.    ClientHeight    =   4275
  7.    ClientLeft      =   165
  8.    ClientTop       =   735
  9.    ClientWidth     =   6750
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4275
  12.    ScaleWidth      =   6750
  13.    StartUpPosition =   3  'Windows Default
  14.    Begin VB.PictureBox picDrawStyleSample 
  15.       Height          =   375
  16.       Index           =   0
  17.       Left            =   2520
  18.       ScaleHeight     =   315
  19.       ScaleWidth      =   720
  20.       TabIndex        =   11
  21.       Top             =   2400
  22.       Visible         =   0   'False
  23.       Width           =   780
  24.    End
  25.    Begin VB.PictureBox picDrawStyle 
  26.       Height          =   375
  27.       Left            =   2520
  28.       ScaleHeight     =   315
  29.       ScaleWidth      =   720
  30.       TabIndex        =   10
  31.       Top             =   2880
  32.       Width           =   780
  33.    End
  34.    Begin VB.PictureBox picFillStyleSample 
  35.       Height          =   375
  36.       Index           =   0
  37.       Left            =   3360
  38.       ScaleHeight     =   315
  39.       ScaleWidth      =   720
  40.       TabIndex        =   9
  41.       Top             =   2400
  42.       Visible         =   0   'False
  43.       Width           =   780
  44.    End
  45.    Begin VB.PictureBox picFillStyle 
  46.       Height          =   375
  47.       Left            =   3360
  48.       ScaleHeight     =   315
  49.       ScaleWidth      =   720
  50.       TabIndex        =   8
  51.       Top             =   2880
  52.       Width           =   780
  53.    End
  54.    Begin VB.PictureBox picDrawWidthSample 
  55.       Height          =   375
  56.       Index           =   0
  57.       Left            =   1680
  58.       ScaleHeight     =   315
  59.       ScaleWidth      =   720
  60.       TabIndex        =   7
  61.       Top             =   2400
  62.       Visible         =   0   'False
  63.       Width           =   780
  64.    End
  65.    Begin VB.PictureBox picDrawWidth 
  66.       Height          =   375
  67.       Left            =   1680
  68.       ScaleHeight     =   315
  69.       ScaleWidth      =   720
  70.       TabIndex        =   6
  71.       Top             =   2880
  72.       Width           =   780
  73.    End
  74.    Begin VB.PictureBox picColorSamples 
  75.       Height          =   615
  76.       Left            =   0
  77.       ScaleHeight     =   555
  78.       ScaleWidth      =   555
  79.       TabIndex        =   3
  80.       Top             =   2520
  81.       Width           =   615
  82.       Begin VB.PictureBox picForeColorSample 
  83.          AutoRedraw      =   -1  'True
  84.          Height          =   255
  85.          Left            =   120
  86.          ScaleHeight     =   195
  87.          ScaleWidth      =   195
  88.          TabIndex        =   4
  89.          Top             =   120
  90.          Width           =   255
  91.       End
  92.       Begin VB.PictureBox picFillColorSample 
  93.          AutoRedraw      =   -1  'True
  94.          Height          =   255
  95.          Left            =   240
  96.          ScaleHeight     =   195
  97.          ScaleWidth      =   195
  98.          TabIndex        =   5
  99.          Top             =   240
  100.          Width           =   255
  101.       End
  102.    End
  103.    Begin VB.PictureBox picSwatch 
  104.       Height          =   255
  105.       Index           =   0
  106.       Left            =   840
  107.       ScaleHeight     =   195
  108.       ScaleWidth      =   195
  109.       TabIndex        =   2
  110.       Top             =   2880
  111.       Width           =   255
  112.    End
  113.    Begin VB.PictureBox picCanvas 
  114.       AutoRedraw      =   -1  'True
  115.       BackColor       =   &H00FFFFFF&
  116.       FillColor       =   &H00C0C0C0&
  117.       Height          =   495
  118.       Left            =   0
  119.       ScaleHeight     =   435
  120.       ScaleWidth      =   435
  121.       TabIndex        =   1
  122.       Top             =   840
  123.       Width           =   495
  124.    End
  125.    Begin ComctlLib.Toolbar tbrButtons 
  126.       Align           =   1  'Align Top
  127.       Height          =   630
  128.       Left            =   0
  129.       TabIndex        =   0
  130.       Top             =   0
  131.       Width           =   6750
  132.       _ExtentX        =   11906
  133.       _ExtentY        =   1111
  134.       ButtonWidth     =   609
  135.       ButtonHeight    =   953
  136.       Appearance      =   1
  137.       _Version        =   393216
  138.    End
  139.    Begin ComctlLib.ImageList imlButtons 
  140.       Left            =   960
  141.       Top             =   1320
  142.       _ExtentX        =   1005
  143.       _ExtentY        =   1005
  144.       BackColor       =   -2147483643
  145.       ImageWidth      =   16
  146.       ImageHeight     =   16
  147.       MaskColor       =   12632256
  148.       _Version        =   393216
  149.       BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628} 
  150.          NumListImages   =   8
  151.          BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  152.             Picture         =   "BitEdit.frx":0000
  153.             Key             =   ""
  154.          EndProperty
  155.          BeginProperty ListImage2 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  156.             Picture         =   "BitEdit.frx":0112
  157.             Key             =   ""
  158.          EndProperty
  159.          BeginProperty ListImage3 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  160.             Picture         =   "BitEdit.frx":0224
  161.             Key             =   ""
  162.          EndProperty
  163.          BeginProperty ListImage4 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  164.             Picture         =   "BitEdit.frx":0336
  165.             Key             =   ""
  166.          EndProperty
  167.          BeginProperty ListImage5 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  168.             Picture         =   "BitEdit.frx":0448
  169.             Key             =   ""
  170.          EndProperty
  171.          BeginProperty ListImage6 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  172.             Picture         =   "BitEdit.frx":055A
  173.             Key             =   ""
  174.          EndProperty
  175.          BeginProperty ListImage7 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  176.             Picture         =   "BitEdit.frx":066C
  177.             Key             =   ""
  178.          EndProperty
  179.          BeginProperty ListImage8 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  180.             Picture         =   "BitEdit.frx":077E
  181.             Key             =   ""
  182.          EndProperty
  183.       EndProperty
  184.    End
  185.    Begin MSComDlg.CommonDialog dlgFile 
  186.       Left            =   2160
  187.       Top             =   1320
  188.       _ExtentX        =   847
  189.       _ExtentY        =   847
  190.       _Version        =   393216
  191.       Filter          =   "Bitmap Files (*.bmp)|*.bmp"
  192.    End
  193.    Begin VB.Menu mnuFile 
  194.       Caption         =   "&File"
  195.       Begin VB.Menu mnuFileNew 
  196.          Caption         =   "&New"
  197.          Shortcut        =   ^N
  198.       End
  199.       Begin VB.Menu mnuFileOpen 
  200.          Caption         =   "&Open"
  201.          Shortcut        =   ^O
  202.       End
  203.       Begin VB.Menu mnuFileSave 
  204.          Caption         =   "&Save"
  205.          Shortcut        =   ^S
  206.       End
  207.       Begin VB.Menu mnuFileSaveAs 
  208.          Caption         =   "Save &As"
  209.       End
  210.       Begin VB.Menu mnuFileSep 
  211.          Caption         =   "-"
  212.       End
  213.       Begin VB.Menu mnuFileExit 
  214.          Caption         =   "E&xit"
  215.       End
  216.    End
  217.    Begin VB.Menu mnuEdit 
  218.       Caption         =   "&Edit"
  219.       Begin VB.Menu mnuEditUndo 
  220.          Caption         =   "&Undo"
  221.          Shortcut        =   ^Z
  222.       End
  223.       Begin VB.Menu mnuEditRedo 
  224.          Caption         =   "&Redo"
  225.          Shortcut        =   ^Y
  226.       End
  227.    End
  228.    Begin VB.Menu mnuDrawWidth 
  229.       Caption         =   "Draw&Width"
  230.       Begin VB.Menu mnuDrawWidthSet 
  231.          Caption         =   "1"
  232.          Index           =   1
  233.       End
  234.       Begin VB.Menu mnuDrawWidthSet 
  235.          Caption         =   "2"
  236.          Index           =   2
  237.       End
  238.       Begin VB.Menu mnuDrawWidthSet 
  239.          Caption         =   "3"
  240.          Index           =   3
  241.       End
  242.       Begin VB.Menu mnuDrawWidthSet 
  243.          Caption         =   "4"
  244.          Index           =   4
  245.       End
  246.       Begin VB.Menu mnuDrawWidthSet 
  247.          Caption         =   "5"
  248.          Index           =   5
  249.       End
  250.    End
  251.    Begin VB.Menu mnuDrawStyle 
  252.       Caption         =   "Draw&Style"
  253.       Begin VB.Menu mnuDrawStyleSet 
  254.          Caption         =   "0"
  255.          Index           =   0
  256.       End
  257.       Begin VB.Menu mnuDrawStyleSet 
  258.          Caption         =   "1"
  259.          Index           =   1
  260.       End
  261.       Begin VB.Menu mnuDrawStyleSet 
  262.          Caption         =   "2"
  263.          Index           =   2
  264.       End
  265.       Begin VB.Menu mnuDrawStyleSet 
  266.          Caption         =   "3"
  267.          Index           =   3
  268.       End
  269.       Begin VB.Menu mnuDrawStyleSet 
  270.          Caption         =   "4"
  271.          Index           =   4
  272.       End
  273.       Begin VB.Menu mnuDrawStyleSet 
  274.          Caption         =   "5"
  275.          Index           =   5
  276.       End
  277.    End
  278.    Begin VB.Menu mnuFillStyle 
  279.       Caption         =   "&FillStyle"
  280.       Begin VB.Menu mnuFillStyleSet 
  281.          Caption         =   "0"
  282.          Index           =   0
  283.       End
  284.       Begin VB.Menu mnuFillStyleSet 
  285.          Caption         =   "1"
  286.          Index           =   1
  287.       End
  288.       Begin VB.Menu mnuFillStyleSet 
  289.          Caption         =   "2"
  290.          Index           =   2
  291.       End
  292.       Begin VB.Menu mnuFillStyleSet 
  293.          Caption         =   "3"
  294.          Index           =   3
  295.       End
  296.       Begin VB.Menu mnuFillStyleSet 
  297.          Caption         =   "4"
  298.          Index           =   4
  299.       End
  300.       Begin VB.Menu mnuFillStyleSet 
  301.          Caption         =   "5"
  302.          Index           =   5
  303.       End
  304.       Begin VB.Menu mnuFillStyleSet 
  305.          Caption         =   "6"
  306.          Index           =   6
  307.       End
  308.       Begin VB.Menu mnuFillStyleSet 
  309.          Caption         =   "7"
  310.          Index           =   7
  311.       End
  312.    End
  313. End
  314. Attribute VB_Name = "Form1"
  315. Attribute VB_GlobalNameSpace = False
  316. Attribute VB_Creatable = False
  317. Attribute VB_PredeclaredId = True
  318. Attribute VB_Exposed = False
  319. Option Explicit
  320.  
  321. ' Tool variables.
  322. Private Enum ToolTypes
  323.     tool_Point = 1
  324.     tool_Line
  325.     tool_Rectangle
  326.     tool_Ellipse
  327.     tool_Scribble
  328.     tool_Polyline
  329.     tool_Undo
  330.     tool_Redo
  331. End Enum
  332. Private SelectedTool As Integer
  333.  
  334. ' Undo/redo variables.
  335. Private Const NUM_UNDOS = 10
  336. Private LastCheckpoint As Integer
  337. Private Checkpoints As Collection
  338.  
  339. ' File variables.
  340. Private FileName As String
  341. Private FileTitle As String
  342.  
  343. ' Drawing variables.
  344. Private Drawing As Boolean
  345. Private FirstX As Single
  346. Private FirstY As Single
  347. Private LastX As Single
  348. Private LastY As Single
  349.  
  350. Private DataModified As Boolean
  351.  
  352. ' API stuff for putting bitmaps in menus.
  353. Private Type MENUITEMINFO
  354.     cbSize As Long
  355.     fMask As Long
  356.     fType As Long
  357.     fState As Long
  358.     wid As Long
  359.     hSubMenu As Long
  360.     hbmpChecked As Long
  361.     hbmpUnchecked As Long
  362.     dwItemData As Long
  363.     dwTypeData As Long
  364.     cch As Long
  365. End Type
  366.  
  367. Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
  368. Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
  369. Private Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal bypos As Long, lpcMenuItemInfo As MENUITEMINFO) As Long
  370.  
  371. Private Const MF_BITMAP = &H4&
  372. Private Const MFT_BITMAP = MF_BITMAP
  373. Private Const MIIM_TYPE = &H10
  374.  
  375. ' See if it is safe to discard the data.
  376. Private Function DataSafe() As Boolean
  377.     If Not DataModified Then
  378.         ' The data has not been modified. It's safe.
  379.         DataSafe = True
  380.     Else
  381.         ' Ask the user if we should save changes.
  382.         Select Case MsgBox("The data has been modified. Do you want to save the changes?", vbYesNoCancel)
  383.             Case vbYes
  384.                 ' Save the data.
  385.                 'mnuFileSave_Click
  386.                 DataSafe = Not DataModified
  387.             Case vbNo
  388.                 DataSafe = True
  389.             Case vbCancel
  390.                 DataSafe = False
  391.         End Select
  392.     End If
  393. End Function
  394.  
  395. ' Draw a color sample.
  396. Private Sub DrawSample()
  397.     picFillColorSample.Line (0, 0)-(1000, 1000), picCanvas.FillColor, BF
  398.     picForeColorSample.Line (0, 0)-(1000, 1000), picCanvas.ForeColor, BF
  399. End Sub
  400.  
  401. ' Draw the shape for the selected tool.
  402. Private Sub DrawShape()
  403. Dim cx As Single
  404. Dim cy As Single
  405. Dim wid As Single
  406. Dim hgt As Single
  407.  
  408.     Select Case SelectedTool
  409.         Case tool_Point
  410.             picCanvas.PSet (LastX, LastY)
  411.         Case tool_Line
  412.             picCanvas.Line (FirstX, FirstY)-(LastX, LastY)
  413.         Case tool_Rectangle
  414.             picCanvas.Line (FirstX, FirstY)-(LastX, LastY), , B
  415.         Case tool_Ellipse
  416.             wid = Abs(LastX - FirstX)
  417.             hgt = Abs(LastY - FirstY)
  418.             If wid = 0 Or hgt = 0 Then Exit Sub
  419.             cx = (FirstX + LastX) / 2
  420.             cy = (FirstY + LastY) / 2
  421.             If wid > hgt Then
  422.                 picCanvas.Circle (cx, cy), wid / 2, , , , hgt / wid
  423.             Else
  424.                 picCanvas.Circle (cx, cy), hgt / 2, , , , hgt / wid
  425.             End If
  426.         Case tool_Scribble
  427.             picCanvas.Line -(LastX, LastY)
  428.         Case tool_Polyline
  429.             picCanvas.Line (FirstX, FirstY)-(LastX, LastY)
  430.     End Select
  431. End Sub
  432. ' Set DataModified = True to indicate the data has
  433. ' been changed. Save the changes for undo/redo.
  434. Private Sub SetModified()
  435.     ' Update the caption if necessary.
  436.     If Not DataModified Then Caption = "BitEdit*[" & FileTitle & "]"
  437.     DataModified = True
  438. End Sub
  439. ' Save the picture for undo/redo.
  440. Private Sub SaveCheckpoint()
  441. Dim new_picture As StdPicture
  442. Dim i As Integer
  443.  
  444.     ' Get the next checkpoint index.
  445.     LastCheckpoint = LastCheckpoint + 1
  446.  
  447.     ' Remove any checkpoints after the current one.
  448.     Do While Checkpoints.Count >= LastCheckpoint
  449.         Checkpoints.Remove Checkpoints.Count
  450.     Loop
  451.  
  452.     ' See if we have too many stored.
  453.     If LastCheckpoint > NUM_UNDOS Then
  454.         ' Too many. Drop the oldest image.
  455.         Checkpoints.Remove 1
  456.         LastCheckpoint = LastCheckpoint - 1
  457.     End If
  458.  
  459.     ' Save the current image.
  460.     picCanvas.Picture = picCanvas.Image
  461.     Set new_picture = New StdPicture
  462.     Set new_picture = picCanvas.Picture
  463.     Checkpoints.Add new_picture
  464.  
  465.     ' Enable and disable the undo buttons.
  466.     SetUndoButtons
  467. End Sub
  468. ' Enable the appropriate undo buttons.
  469. Private Sub SetUndoButtons()
  470. Dim enable_undo As Boolean
  471. Dim enable_redo As Boolean
  472.  
  473.     enable_undo = (LastCheckpoint > 1)
  474.     enable_redo = (LastCheckpoint < Checkpoints.Count)
  475.  
  476.     If enable_undo <> mnuEditUndo.Enabled Then
  477.         tbrButtons.Buttons("Undo").Enabled = enable_undo
  478.         mnuEditUndo.Enabled = enable_undo
  479.     End If
  480.  
  481.     If enable_redo <> mnuEditRedo.Enabled Then
  482.         tbrButtons.Buttons("Redo").Enabled = enable_redo
  483.         mnuEditRedo.Enabled = enable_redo
  484.     End If
  485. End Sub
  486.  
  487. Private Sub Form_Load()
  488. Dim btn As Button
  489. Dim i As Integer
  490. Dim tips(tool_Point To tool_Redo) As String
  491. Dim pos As Single
  492. Dim main_menu As Long
  493. Dim sub_menu As Long
  494. Dim menu_info As MENUITEMINFO
  495.  
  496.     dlgFile.InitDir = App.Path
  497.  
  498.     ' Load the tool tips.
  499.     tips(tool_Point) = "Point"
  500.     tips(tool_Line) = "Line"
  501.     tips(tool_Rectangle) = "Rectangle"
  502.     tips(tool_Ellipse) = "Ellipse"
  503.     tips(tool_Scribble) = "Scribble"
  504.     tips(tool_Polyline) = "Polyline"
  505.     tips(tool_Undo) = "Undo"
  506.     tips(tool_Redo) = "Redo"
  507.  
  508.     ' Load the tool buttons.
  509.     tbrButtons.ImageList = imlButtons
  510.     For i = tool_Point To tool_Redo
  511.         Set btn = tbrButtons.Buttons.Add(, , , , i)
  512.         btn.ToolTipText = tips(i)
  513.         btn.Key = tips(i)
  514.     Next i
  515.  
  516.     ' Create color swatches.
  517.     For i = 0 To 15
  518.         If i > 0 Then
  519.             Load picSwatch(i)
  520.             picSwatch(i).Visible = True
  521.         End If
  522.         picSwatch(i).BackColor = QBColor(i)
  523.     Next i
  524.     picColorSamples.Height = 2 * picSwatch(0).Height + 30
  525.     picColorSamples.Width = picColorSamples.Height
  526.     pos = picColorSamples.ScaleWidth * 0.1
  527.     picForeColorSample.Move pos, pos
  528.     pos = picColorSamples.ScaleWidth * 0.9 - picFillColorSample.Width
  529.     picFillColorSample.Move pos, pos
  530.  
  531.     ' Create the DrawWidth menu.
  532.     main_menu = GetMenu(hwnd)
  533.     sub_menu = GetSubMenu(main_menu, 2)
  534.     For i = 1 To 5
  535.         Load picDrawWidthSample(i)
  536.         picDrawWidthSample(i).AutoRedraw = True
  537.         picDrawWidthSample(i).DrawWidth = i
  538.         picDrawWidthSample(i).Line (-1000, picDrawWidthSample(0).ScaleHeight / 2)-Step(2000, 0)
  539.         picDrawWidthSample(i).Picture = picDrawWidthSample(i).Image
  540.         With menu_info
  541.             .cbSize = Len(menu_info)
  542.             .fMask = MIIM_TYPE
  543.             .fType = MFT_BITMAP
  544.             .dwTypeData = picDrawWidthSample(i).Picture
  545.         End With
  546.         SetMenuItemInfo sub_menu, i - 1, True, menu_info
  547.     Next i
  548.     ' Start with DrawWidth = 1.
  549.     mnuDrawWidthSet_Click 1
  550.  
  551.     ' Create the DrawStyle menu.
  552.     main_menu = GetMenu(hwnd)
  553.     sub_menu = GetSubMenu(main_menu, 3)
  554.     For i = 0 To 5
  555.         If i > 0 Then Load picDrawStyleSample(i)
  556.         picDrawStyleSample(i).AutoRedraw = True
  557.         picDrawStyleSample(i).Line (0, 0)-(2000, 2000), picDrawStyleSample(0).BackColor, BF
  558.         picDrawStyleSample(i).DrawStyle = i
  559.         picDrawStyleSample(i).Line (-1000, picDrawStyleSample(0).ScaleHeight / 2)-Step(2000, 0)
  560.         picDrawStyleSample(i).Picture = picDrawStyleSample(i).Image
  561.         With menu_info
  562.             .cbSize = Len(menu_info)
  563.             .fMask = MIIM_TYPE
  564.             .fType = MFT_BITMAP
  565.             .dwTypeData = picDrawStyleSample(i).Picture
  566.         End With
  567.         SetMenuItemInfo sub_menu, i, True, menu_info
  568.     Next i
  569.     ' Start with Drawstyle = vbSolid.
  570.     mnuDrawStyleSet_Click vbSolid
  571.  
  572.     ' Create the fillstyle menu.
  573.     main_menu = GetMenu(hwnd)
  574.     sub_menu = GetSubMenu(main_menu, 4)
  575.     For i = 0 To 7
  576.         If i > 0 Then Load picFillStyleSample(i)
  577.         picFillStyleSample(i).AutoRedraw = True
  578.         picFillStyleSample(i).FillStyle = vbFSSolid
  579.         picFillStyleSample(i).Line (-1000, -1000)-(2000, 2000), picFillStyleSample(0).BackColor, BF
  580.         picFillStyleSample(i).FillStyle = i
  581.         picFillStyleSample(i).Line (-1000, -1000)-(2000, 2000), , B
  582.         picFillStyleSample(i).Picture = picFillStyleSample(i).Image
  583.         With menu_info
  584.             .cbSize = Len(menu_info)
  585.             .fMask = MIIM_TYPE
  586.             .fType = MFT_BITMAP
  587.             .dwTypeData = picFillStyleSample(i).Picture
  588.         End With
  589.         SetMenuItemInfo sub_menu, i, True, menu_info
  590.     Next i
  591.     ' Start with fillstyle = vbFSTransparent.
  592.     mnuFillStyleSet_Click vbFSTransparent
  593.  
  594.     ' Start a new project.
  595.     mnuFileNew_Click
  596.  
  597.     ' Draw the initial sample.
  598.     DrawSample
  599.  
  600.     ' Select the point tool.
  601.     tbrButtons_ButtonClick tbrButtons.Buttons(tool_Point)
  602. End Sub
  603. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  604.     Cancel = Not DataSafe
  605. End Sub
  606.  
  607. Private Sub Form_Resize()
  608. Dim hgt As Single
  609. Dim t As Single
  610. Dim i As Integer
  611.  
  612.     If WindowState = vbMinimized Then Exit Sub
  613.  
  614.     t = ScaleHeight - picColorSamples.Height
  615.     picColorSamples.Top = t
  616.  
  617.     picSwatch(0).Move picColorSamples.Width + 30, picColorSamples.Top
  618.     For i = 0 To 15
  619.         picSwatch(i).Visible = False
  620.     Next i
  621.     For i = 1 To 15
  622.         If i = 8 Then
  623.             picSwatch(i).Left = picSwatch(0).Left
  624.             picSwatch(i).Top = picSwatch(0).Top + picSwatch(0).Height + 30
  625.         Else
  626.             picSwatch(i).Left = picSwatch(i - 1).Left + picSwatch(i - 1).Width + 30
  627.             picSwatch(i).Top = picSwatch(i - 1).Top
  628.         End If
  629.     Next i
  630.     For i = 0 To 15
  631.         picSwatch(i).Visible = True
  632.     Next i
  633.  
  634.     hgt = picColorSamples.Top - tbrButtons.Height - 30
  635.     If hgt <= 0 Then Exit Sub
  636.     picCanvas.Move 0, tbrButtons.Height, ScaleWidth, hgt
  637.  
  638.     picDrawWidth.Move picSwatch(7).Left + _
  639.         picSwatch(7).Width + 120, _
  640.         picSwatch(7).Top
  641.     picDrawStyle.Move picDrawWidth.Left + _
  642.         picDrawWidth.Width + 120, _
  643.         picDrawWidth.Top
  644.     picFillStyle.Move picDrawStyle.Left + _
  645.         picDrawStyle.Width + 120, _
  646.         picDrawStyle.Top
  647. End Sub
  648.  
  649. ' Set the DrawStyle.
  650. Private Sub mnuDrawStyleSet_Click(Index As Integer)
  651. Dim i As Integer
  652.  
  653.     ' Check the selected style.
  654.     For i = 0 To 5
  655.         mnuDrawStyleSet(i).Checked = False
  656.     Next i
  657.     mnuDrawStyleSet(Index).Checked = True
  658.  
  659.     ' Display the selected style.
  660.     picDrawStyle.Picture = picDrawStyleSample(Index).Picture
  661.  
  662.     ' Select the DrawStyle.
  663.     picCanvas.DrawStyle = Index
  664. End Sub
  665.  
  666. ' Redo the previously undone command.
  667. Private Sub mnuEditRedo_Click()
  668.     LastCheckpoint = LastCheckpoint + 1
  669.     picCanvas.Picture = Checkpoints(LastCheckpoint)
  670.     SetUndoButtons
  671.  
  672.     ' Flag the data as modified.
  673.     SetModified
  674. End Sub
  675.  
  676. ' Undo the previous command.
  677. Private Sub mnuEditUndo_Click()
  678.     LastCheckpoint = LastCheckpoint - 1
  679.     picCanvas.Picture = Checkpoints(LastCheckpoint)
  680.  
  681.     ' Enable and disable the undo buttons.
  682.     SetUndoButtons
  683.  
  684.     ' Flag the data as modified.
  685.     SetModified
  686. End Sub
  687.  
  688. ' Unload the form. The QueryUnload event handler
  689. ' will make sure it's safe to do so.
  690. Private Sub mnuFileExit_Click()
  691.     Unload Me
  692. End Sub
  693.  
  694. ' Start a new project.
  695. Private Sub mnuFileNew_Click()
  696.     ' Make sure the data is safe.
  697.     If Not DataSafe() Then Exit Sub
  698.  
  699.     ' Start a new project.
  700.     picCanvas.Line (0, 0)-(picCanvas.ScaleWidth, picCanvas.ScaleHeight), picCanvas.BackColor, BF
  701.  
  702.     ' Start a new Checkpoints collection.
  703.     Set Checkpoints = New Collection
  704.     LastCheckpoint = 0
  705.  
  706.     ' Checkpoint the blank project.
  707.     SaveCheckpoint
  708.  
  709.     DataModified = False
  710.     Caption = "BitEdit []"
  711.     FileName = ""
  712.     FileTitle = ""
  713. End Sub
  714.  
  715. ' Open a file.
  716. Private Sub mnuFileOpen_Click()
  717.     ' Make sure the data is safe.
  718.     If Not DataSafe() Then Exit Sub
  719.  
  720.     ' Let the user select a file name.
  721.     dlgFile.Flags = _
  722.         cdlOFNExplorer + _
  723.         cdlOFNHideReadOnly + _
  724.         cdlOFNLongNames + _
  725.         cdlOFNFileMustExist
  726.     dlgFile.CancelError = True
  727.     On Error Resume Next
  728.     dlgFile.ShowOpen
  729.     If Err.Number = cdlCancel Then
  730.         Exit Sub
  731.     ElseIf Err.Number > 0 Then
  732.         MsgBox "Error " & Format$(Err.Number) & _
  733.             " selecting the file." & _
  734.             vbCrLf & Err.Description
  735.         Exit Sub
  736.     End If
  737.     On Error GoTo 0
  738.  
  739.     ' Open the file.
  740.     On Error GoTo OpenErr
  741.     picCanvas.Picture = LoadPicture(dlgFile.FileName)
  742.  
  743.     ' Start a new Checkpoints collection.
  744.     Set Checkpoints = New Collection
  745.     LastCheckpoint = 0
  746.  
  747.     ' Checkpoint the new file.
  748.     SaveCheckpoint
  749.  
  750.     ' Update the file name and title.
  751.     FileName = dlgFile.FileName
  752.     FileTitle = dlgFile.FileTitle
  753.     Caption = "BitEdit [" & FileTitle & "]"
  754.     DataModified = False
  755.     Exit Sub
  756.  
  757. OpenErr:
  758.     MsgBox "Error " & Format$(Err.Number) & _
  759.         " saving file '" & dlgFile.FileName & "'." & _
  760.         vbCrLf & Err.Description
  761.     Exit Sub
  762.  
  763.  
  764.     ' Update the file name and title.
  765.     FileName = dlgFile.FileName
  766.     FileTitle = dlgFile.FileTitle
  767.     Caption = "BitEdit [" & FileTitle & "]"
  768.     DataModified = False
  769.  
  770. End Sub
  771.  
  772. ' Save the file.
  773. Private Sub mnuFileSave_Click()
  774.     ' If there is no file name, treat as Save As.
  775.     If Len(FileName) = 0 Then
  776.         mnuFileSaveAs_Click
  777.         Exit Sub
  778.     End If
  779.  
  780.     ' Save the file.
  781.     On Error GoTo SaveErr
  782.     SavePicture picCanvas.Picture, FileName
  783.  
  784.     ' Update the file name and title.
  785.     FileName = dlgFile.FileName
  786.     FileTitle = dlgFile.FileTitle
  787.     Caption = "BitEdit [" & FileTitle & "]"
  788.     DataModified = False
  789.     Exit Sub
  790.  
  791. SaveErr:
  792.     MsgBox "Error " & Format$(Err.Number) & _
  793.         " saving file '" & FileName & "'." & _
  794.         vbCrLf & Err.Description
  795.     Exit Sub
  796. End Sub
  797. ' Save the file with a new name.
  798. Private Sub mnuFileSaveAs_Click()
  799.     ' Let the user select a file name.
  800.     dlgFile.Flags = _
  801.         cdlOFNExplorer + _
  802.         cdlOFNHideReadOnly + _
  803.         cdlOFNLongNames + _
  804.         cdlOFNOverwritePrompt + _
  805.         cdlOFNPathMustExist
  806.     dlgFile.CancelError = True
  807.     On Error Resume Next
  808.     dlgFile.ShowSave
  809.     If Err.Number = cdlCancel Then
  810.         Exit Sub
  811.     ElseIf Err.Number > 0 Then
  812.         MsgBox "Error " & Format$(Err.Number) & _
  813.             " selecting the file." & _
  814.             vbCrLf & Err.Description
  815.         Exit Sub
  816.     End If
  817.     On Error GoTo 0
  818.  
  819.     ' Save the file.
  820.     On Error GoTo SaveAsErr
  821.     SavePicture picCanvas.Picture, dlgFile.FileName
  822.  
  823.     ' Update the file name and title.
  824.     FileName = dlgFile.FileName
  825.     FileTitle = dlgFile.FileTitle
  826.     Caption = "BitEdit [" & FileTitle & "]"
  827.     DataModified = False
  828.     Exit Sub
  829.  
  830. SaveAsErr:
  831.     MsgBox "Error " & Format$(Err.Number) & _
  832.         " saving file '" & dlgFile.FileName & "'." & _
  833.         vbCrLf & Err.Description
  834.     Exit Sub
  835. End Sub
  836.  
  837. ' Set the DrawWidth.
  838. Private Sub mnuDrawWidthSet_Click(Index As Integer)
  839. Dim i As Integer
  840.  
  841.     ' Check the selected width.
  842.     For i = 1 To 5
  843.         mnuDrawWidthSet(i).Checked = False
  844.     Next i
  845.     mnuDrawWidthSet(Index).Checked = True
  846.  
  847.     ' Display the selected width.
  848.     picDrawWidth.Picture = picDrawWidthSample(Index).Picture
  849.  
  850.     ' Select the DrawWidth.
  851.     picCanvas.DrawWidth = Index
  852. End Sub
  853.  
  854. ' Set the FillStyle.
  855. Private Sub mnuFillStyleSet_Click(Index As Integer)
  856. Dim i As Integer
  857.  
  858.     ' Check the selected style.
  859.     For i = 0 To 7
  860.         mnuFillStyleSet(i).Checked = False
  861.     Next i
  862.     mnuFillStyleSet(Index).Checked = True
  863.  
  864.     ' Display the selected style.
  865.     picFillStyle.Picture = picFillStyleSample(Index).Picture
  866.  
  867.     ' Select the fillstyle.
  868.     picCanvas.FillStyle = Index
  869. End Sub
  870.  
  871. ' Start doing something.
  872. Private Sub picCanvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  873.     ' See if we are ending a polyline.
  874.     If (SelectedTool = tool_Polyline) And _
  875.        (Button = vbRightButton) And Drawing _
  876.     Then
  877.         ' End the polyline.
  878.         Drawing = False
  879.  
  880.         ' Erase the last segment.
  881.         DrawShape
  882.  
  883.         ' Mark the data and save a checkpoint.
  884.         SetModified
  885.         SaveCheckpoint
  886.         Exit Sub
  887.     End If
  888.  
  889.     ' See if we are drawing a polyline.
  890.     If (SelectedTool = tool_Polyline) And Drawing Then
  891.         ' Finalize the segment.
  892.         picCanvas.DrawMode = vbCopyPen
  893.         DrawShape
  894.     End If
  895.  
  896.     ' Deal with other situations.
  897.     ' Save the coordinates.
  898.     FirstX = X
  899.     FirstY = Y
  900.     LastX = X
  901.     LastY = Y
  902.     
  903.     ' Prepare to draw in invert mode.
  904.     If SelectedTool = tool_Scribble Then
  905.         picCanvas.CurrentX = X
  906.         picCanvas.CurrentY = Y
  907.     ElseIf SelectedTool = tool_Polyline Then
  908.         ' See if we are not already drawing.
  909.         If Not Drawing Then
  910.             ' Start the first segment here.
  911.             picCanvas.CurrentX = X
  912.             picCanvas.CurrentY = Y
  913.         End If
  914.         picCanvas.DrawMode = vbInvert
  915.     Else
  916.         picCanvas.DrawMode = vbInvert
  917.     End If
  918.     Drawing = True
  919.  
  920.     ' Draw the initial shape.
  921.     DrawShape
  922. End Sub
  923. Private Sub picCanvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  924.     If Not Drawing Then Exit Sub
  925.  
  926.     ' Erase the previous shape.
  927.     DrawShape
  928.  
  929.     LastX = X
  930.     LastY = Y
  931.  
  932.     ' Draw the new shape.
  933.     DrawShape
  934. End Sub
  935.  
  936.  
  937. Private Sub picCanvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  938.     If Not Drawing Then Exit Sub
  939.  
  940.     ' Do nothing if we are drawing a polyline.
  941.     ' All the interesting stuff happens in the
  942.     ' MouseDown and MouseMove event handlers.
  943.     If SelectedTool <> tool_Polyline Then
  944.         Drawing = False
  945.  
  946.         ' Erase the previous shape.
  947.         DrawShape
  948.  
  949.         LastX = X
  950.         LastY = Y
  951.  
  952.         ' Draw the final shape.
  953.         picCanvas.DrawMode = vbCopyPen
  954.         DrawShape
  955.  
  956.         ' Mark the data and save a checkpoint.
  957.         SetModified
  958.         SaveCheckpoint
  959.     End If
  960. End Sub
  961.  
  962. ' Display the DrawStyle popup.
  963. Private Sub picDrawStyle_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  964.     PopupMenu mnuDrawStyle
  965. End Sub
  966.  
  967.  
  968. ' Display the DrawWidth popup.
  969. Private Sub picDrawWidth_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  970.     PopupMenu mnuDrawWidth
  971. End Sub
  972.  
  973. ' Display the FillStyle popup.
  974. Private Sub picFillStyle_Click()
  975.     PopupMenu mnuFillStyle
  976. End Sub
  977.  
  978. ' Select the new color.
  979. Private Sub picSwatch_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
  980.     If Button = vbLeftButton Then
  981.         picCanvas.ForeColor = QBColor(Index)
  982.     Else
  983.         picCanvas.FillColor = QBColor(Index)
  984.     End If
  985.  
  986.     ' Draw a new color sample.
  987.     DrawSample
  988. End Sub
  989.  
  990.  
  991. ' Process a toolbar button click.
  992. Private Sub tbrButtons_ButtonClick(ByVal Button As ComctlLib.Button)
  993.     ' See what kind of button this is.
  994.     If Button.Index <= tool_Polyline Then
  995.         ' This is a toggle button.
  996.         ' Deselect the previously selected tool.
  997.         If SelectedTool > 0 Then tbrButtons.Buttons(SelectedTool).Value = tbrUnpressed
  998.  
  999.         ' Select the new tool.
  1000.         SelectedTool = Button.Index
  1001.         tbrButtons.Buttons(SelectedTool).Value = tbrPressed
  1002.         tbrButtons.Refresh
  1003.     ElseIf Button.Index = tool_Undo Then
  1004.         ' Undo the previous command.
  1005.         mnuEditUndo_Click
  1006.     ElseIf Button.Index = tool_Redo Then
  1007.         ' Redo the previously undone command.
  1008.         mnuEditRedo_Click
  1009.     End If
  1010. End Sub
  1011.